package com.iotteam.androidbase.moudules.login.presenter;

import android.text.TextUtils;

import com.iotteam.androidbase.R;
import com.iotteam.androidbase.commen.network.Api;
import com.iotteam.androidbase.commen.network.core.HttpListener;
import com.iotteam.androidbase.commen.network.core.HttpManager;
import com.iotteam.androidbase.commen.utils.PLog;
import com.iotteam.androidbase.commen.utils.ResUtil;
import com.iotteam.androidbase.commen.utils.ToastUtil;
import com.iotteam.androidbase.moudules.login.view.IRegistView;

import java.util.HashMap;

/**
 * Created by lidechen on 5/7/17.
 */

public class RegistPresenterCompl implements IRegistPresenter {

    private static final String TAG = "RegistPresenterCompl";

    private IRegistView mView;

    private HashMap<String, String> mSmsParam = null;
    private HashMap<String, String> mSignupParam = null;

    public RegistPresenterCompl(IRegistView view){
        mView = view;
    }

    @Override
    public void onSmsSuccess() {

    }

    @Override
    public void onSmsFail() {

    }

    @Override
    public boolean doSmsCode() {
        String mobile = mView.getMobile();
        if(TextUtils.isEmpty(mobile)){
            mView.showError(ResUtil.getStringById(R.string.dialog_mobileerr));
            return false;
        }

        mSmsParam = new HashMap<>();
        mSmsParam.put("mobile", mobile);

        HttpManager.getInstance().postString(Api.sendVerifySMS, mSmsParam, new HttpListener() {
            @Override
            public void onSucceed(int what, Object bean) {

            }

            @Override
            public void onFailed(int what, Exception e) {

            }

            @Override
            public void onStart(int what) {
                mView.showLoading();
            }

            @Override
            public void onFinish(int what) {
                mView.dismissLoading();
            }
        });

        return true;
    }

    @Override
    public void canceldoSmsCode() {
        HttpManager.getInstance().cancelPostBean(mSmsParam);
    }

    @Override
    public void doRegister() {
        String mobile = mView.getMobile();
        String password = mView.getPassword();
        String repassword = mView.getRepassword();
        String smsCode =  mView.getSmscode();

        if(TextUtils.isEmpty(mobile)){
            mView.showError(ResUtil.getStringById(R.string.dialog_mobileerr));
            return;
        }
        if(TextUtils.isEmpty(password)){
            mView.showError(ResUtil.getStringById(R.string.dialog_pwderr));
            return;
        }
        if(password.length() < 6 || password.length() > 16){
            mView.showError(ResUtil.getStringById(R.string.dialog_pwdshorterr));
            return;
        }
        if(!password.equals(repassword)){
            mView.showError(ResUtil.getStringById(R.string.dialog_repwderr));
            return;
        }
        if(TextUtils.isEmpty(smsCode)){
            mView.showError(ResUtil.getStringById(R.string.dialog_smserr));
            return;
        }

        mSignupParam = new HashMap<>();
        mSignupParam.put("mobile", mobile);
        mSignupParam.put("password", password);
        mSignupParam.put("smscode", smsCode);

        HttpManager.getInstance().postString(Api.signup, mSignupParam, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, String bean) {
                PLog.i(TAG, "data : "+bean);
                ToastUtil.showLong(bean);
            }

            @Override
            public void onFailed(int what, Exception e) {
                //mView.showError(ResUtil.getStringById(R.string.dialog_registe_fail));
            }

            @Override
            public void onStart(int what) {
                mView.showLoading();
            }

            @Override
            public void onFinish(int what) {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void canceldoRegister() {
        HttpManager.getInstance().cancelPostBean(mSignupParam);
    }
}
